import requests
from lxml import etree

"""
流程分析：
1. 通过requests发送请求获取贴吧返回的内容
2. 将返回的内容通过etree.HTML转换为Element对象
3. 对Element对象使用XPath提取数据
"""

# 1. 通过requests发送请求获取贴吧返回的内容
url = "https://movie.douban.com/subject/1292052/comments?status=P"
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36",
}
r = requests.get(url=url, headers=headers)

# print(r.text)

# 2. 将返回的内容通过etree.HTML转换为Element对象
html = etree.HTML(r.text)

# 3. 对Element对象使用XPath提取数据
comment_list = html.xpath('//span[@class="short"]/text()')

print("提取到的个数：", len(comment_list))

for comment in comment_list:
    print(comment)
    print()
